package com.pilipili.zeus.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pilipili.commonutils.JwtUtils;
import com.pilipili.commonutils.MD5;
import com.pilipili.servicebase.handler.MyException;
import com.pilipili.zeus.educenter.entity.UcenterMember;
import com.pilipili.zeus.educenter.entity.vo.Register;
import com.pilipili.zeus.educenter.mapper.UcenterMemberMapper;
import com.pilipili.zeus.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * 会员表 服务实现类
 *
 * @author Unlimited_Blade_Works
 * @since 2021-05-31
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Override
    public String login(UcenterMember user) {
        String mobile = user.getMobile();
        String password = user.getPassword();
        if(StringUtils.isEmpty(password)||StringUtils.isEmpty(mobile)){
            throw new MyException(20001,"登录失败");
        }
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        UcenterMember member = baseMapper.selectOne(queryWrapper);
        if(member==null){
            throw new MyException(20001,"用户不存在");
        }
        if(!MD5.encrypt(password).equals(member.getPassword())){
            throw new MyException(20001,"输入的账号或密码有误");
        }
        if(member.getIsDisabled()){
            throw new MyException(20001,"该账号已被禁封");
        }
        return JwtUtils.getJwtToken(member.getId(), member.getNickname());
    }

    @Override
    public void register(Register user) {
        String mobile = user.getMobile();
        String code = user.getCode();
        String nikeName = user.getNickname();
        String password = user.getPassword();
//        if(StringUtils.isEmpty(password)||StringUtils.isEmpty(mobile)||StringUtils.isEmpty(code)||StringUtils.isEmpty(nikeName)){
//            throw new MyException(20001,"参数为空,注册失败");
//        }
//        String redisCode = redisTemplate.opsForValue().get(code);
//        if(!code.equals(redisCode)){
//            throw new MyException(20001,"验证码有误或已失效");
//        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(queryWrapper);
        if(count>0){
            throw new MyException(20001,"手机号已存在");
        }
        UcenterMember member = new UcenterMember();
        user.setPassword(MD5.encrypt(password));
        BeanUtils.copyProperties(user,member);
        baseMapper.insert(member);
    }

    @Override
    public Integer countRegisterNum(String date) {
        return baseMapper.countRegisterUserNumByDate(date);
    }
}
